﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Ext.Net.UIS;
using Ext.Net.UIS.Script;
using tg = Ext.Net.UIS.TermTags;
using ts = Ext.Net.UIS.Script.TermScript;

namespace WebPoligon
{

    public static class Semantic
    {
        const string Month = "Month";
        const string State = "State";
        const string CreditCardType = "CreditCardType";


        public static Term NewSemantic()
        {
            return new Term("App"

                , new Entity(Month, tg.Simple).AddItems(
                      new Text("ID", tg.ID)
                    , new Text("Name", tg.Name) { Length = 9 }
                )
                , new Entity(State, tg.Simple).AddItems(
                      new Text("State", tg.ID, tg.Name) { Length = 2 }
                )
                , new Entity(CreditCardType, tg.Simple).AddItems(
                      new Text("ID", tg.ID)
                    , new Text("Name", tg.Name)
                )

                , NewCheckout()

            );
        }


        #region Checkout

        #region Consts

        public const string Checkout = "Checkout";

        public const string ContactInfo = "ContactInfo";
        public const string FullName = "FullName";
        public const string FirstName = "FirstName";
        public const string LastName = "LastName";
        public const string Email = "Email";
        public const string Phone = "Phone";

        public const string MailingAddress = "MailingAddress";
        public const string MailingStreet = "MailingStreet";
        public const string MailingCity = "MailingCity";
        public const string MailingState = "MailingState";
        public const string MailingPostalCode = "MailingPostalCode";

        public const string BillingAddress = "BillingAddress";
        public const string BillingSameAsMailing = "BillingSameAsMailing";
        public const string BillingStreet = "BillingStreet";
        public const string BillingCity = "BillingCity";
        public const string BillingState = "BillingState";
        public const string BillingPostalCode = "BillingPostalCode";

        public const string Payment = "Payment";
        public const string ccType = "ccType";
        public const string ccName = "ccName";
        public const string ccNumber = "ccNumber";
        public const string ccExpiration = "ccExpiration";
        public const string ccExpireMonth = "ccExpireMonth";
        public const string ccExpireYear = "ccExpireYear";

        #endregion

        private static Entity NewCheckout()
        {
            return new Entity(Checkout
                , new Int("ID", tg.ID)

                , new Group(ContactInfo, "Your Contact Information"
                    , new Union(FullName, "Name"
                        , new Text(FirstName, new TermTitle("First Name", "First")) { Flex = 2 }
                        , new Text(LastName, new TermTitle("Last Name", new { ContactInfo = "Last" })) { Flex = 3 }
                    )
                    , new Text("Name1", "Name").OnCalculate(() => ts.Term(FirstName) + " " + ts.Term(LastName))
                    , new Text("Name1t", "Titled Name").OnCalculate(() => "Mr. " + ts.Term("Name1"))
                    , new Text(Email, "Email Address")
                    , new Text(Phone, new TermTitle("Phone Number") { Empty = "xxx-xxx-xxxx" }) { Length = 12 }
                )

                , new Group(MailingAddress, "Mailing Address"
                    , new Text(MailingStreet, new TermTitle("Mailing Street Address", "Street Address"))
                    , new Text(MailingCity, new TermTitle("Mailing City", "City"))
                    , new Text(MailingState, new TermTitle("Mailing State", "State")).SetLookup(State)
                    , new Text(MailingPostalCode, new TermTitle("Mailing Postal Code", "Postal Code"))
                )

                , new Group(BillingAddress, "Billing Address"
                    , new Bool(BillingSameAsMailing, new TermTitle("Billing Address same as Mailing Address?", "Same as Mailing Address?"), tg.Header)
                        .OnChange(
                            new[] { BillingStreet, BillingCity, BillingState, BillingPostalCode, }.SetEnabled()
                //BillingState.SetEnabled(BillingCity.Enabled()),
                //BillingPostalCode.BindEnabled(BillingSameAsMailing),
                //BillingPostalCode.SetValue(ts.Raw("'111 ' + '222 ' + value")),
                //BillingCity.SetValue(() => (ts.Term(BillingSameAsMailing) + 4) * 2),
                //ts.If(BillingSameAsMailing,
                //    new[] { BillingStreet.SetValue(ts.Raw("'is street'")), BillingCity.SetValue(ts.Raw("''")) },
                //    new[] { BillingCity.SetValue(ts.Raw("'is city'")), BillingStreet.SetValue(ts.Raw("''")) }
                //)
                        )
                    , new Text(BillingStreet, new TermTitle("Billing Street Address", "Street Address"))
                    , new Text(BillingCity, new TermTitle("Billing City", "City"))
                    , new Text(BillingState, new TermTitle("Billing State", "State")).SetLookup(State)
                    , new Text(BillingPostalCode, new TermTitle("Billing Postal Code", "Postal Code"))
                )

                , new Group(Payment
                    , new Int(ccType, "Type").SetLookup(CreditCardType)
                    , new Text(ccName, "Name On Card")
                    , new Text(ccNumber, "Card Number")
                    , new Union(ccExpiration, "Expiration"
                        , new Int(ccExpireMonth, "Month").SetLookup(Month)
                        , new Int(ccExpireYear, "Year") { Length = 4 }
                    )
                )
            );
        }

        #endregion


    }

}